<template>
  <common-layout>
    <div class="top">
      <div class="header">
        <!-- <img alt="logo" class="logo" src="@/assets/img/logo.png" /> -->
        <span class="title">{{ systemName }}</span>
      </div>
      <!-- <div class="desc">Ant Design 是西湖区最具影响力的 Web 设计规范</div> -->
    </div>
    <div class="login">
      <a-form @submit="onSubmit" :form="form">
        <a-tabs
          size="large"
          :tabBarStyle="{ textAlign: 'center' }"
          style="padding: 0 2px"
          :default-active-key="tabIndex"
          @change="callback"
        >
          <a-tab-pane tab="账户密码登录" key="1">
            <div v-if="tabIndex == 1">
              <a-alert
                type="error"
                :closable="true"
                v-show="error"
                :message="error"
                showIcon
                style="margin-bottom: 24px"
              />
              <a-form-item>
                <a-input
                  autocomplete="autocomplete"
                  size="large"
                  placeholder="请输入手机号"
                  v-decorator="[
                    'phone',
                    {
                      rules: [
                        {
                          required: true,
                          message: '请输入用户名或手机号',
                          whitespace: true,
                        },
                      ],
                    },
                  ]"
                >
                  <a-icon slot="prefix" type="user" />
                </a-input>
              </a-form-item>
              <a-form-item>
                <a-input
                  size="large"
                  placeholder="请输入密码"
                  autocomplete="autocomplete"
                  type="password"
                  v-decorator="[
                    'password',
                    {
                      rules: [
                        {
                          required: true,
                          message: '请输入密码',
                          whitespace: true,
                        },
                      ],
                    },
                  ]"
                >
                  <a-icon slot="prefix" type="lock" />
                </a-input>
              </a-form-item>
            </div>
          </a-tab-pane>
          <a-tab-pane tab="手机号登录" key="2">
            <div v-if="tabIndex == 2">
              <a-form-item>
                <a-input
                  size="large"
                  placeholder="请输入手机号码"
                  v-decorator="[
                    'phone1',
                    {
                      rules: [
                        {
                          required: true,
                          message: '请输入手机号',
                          whitespace: true,
                        },
                      ],
                    },
                  ]"
                >
                  <a-icon slot="prefix" type="mobile" />
                </a-input>
              </a-form-item>
              <a-form-item>
                <a-row :gutter="8" style="margin: 0 -4px">
                  <a-col :span="16">
                    <a-input
                      size="large"
                      placeholder="请输入验证码"
                      v-decorator="[
                        'password1',
                        {
                          rules: [
                            {
                              required: true,
                              message: '请输入验证码',
                              whitespace: true,
                            },
                          ],
                        },
                      ]"
                    >
                      <a-icon slot="prefix" type="mail" />
                    </a-input>
                  </a-col>
                  <a-col :span="8" style="padding-left: 4px">
                    <a-button
                      style="width: 100%"
                      class="captcha-button"
                      size="large"
                      @click="getMail"
                      :disabled="disabled"
                    >
                      <span v-show="!disabled"> 获取验证码</span>
                      <span v-show="disabled" style="color: red"
                        >{{ time }}s</span
                      >
                    </a-button>
                  </a-col>
                </a-row>
              </a-form-item>
            </div>
          </a-tab-pane>
        </a-tabs>
        <div>
          <a-checkbox :checked="true" style="color: #ffff">自动登录</a-checkbox>
          <a style="float: right; color: #ffff">
            <a-tooltip placement="topLeft">
              <template slot="title"> 请联系管理员重置密码！ </template>
              <span style="color: red; margin-right: 5px"
                ><a-icon type="question-circle"
              /></span>
            </a-tooltip>

            忘记密码</a
          >
        </div>
        <a-form-item>
          <a-button
            :loading="logging"
            style="width: 100%; margin-top: 24px"
            size="large"
            htmlType="submit"
            type="primary"
            >登录</a-button
          >
        </a-form-item>
        <!-- <div>
          其他登录方式
          <a-icon class="icon" type="alipay-circle" />
          <a-icon class="icon" type="taobao-circle" />
          <a-icon class="icon" type="weibo-circle" />
          <router-link style="float: right" to="/dashboard/workplace" >注册账户</router-link>
        </div> -->
      </a-form>
    </div>
  </common-layout>
</template>

<script>
import CommonLayout from "@/layouts/CommonLayout";
import { getKey, login, getRoutesConfig, getLoginCode } from "@/services/user";
import { setAuthorization } from "@/utils/request";
import { loadRoutes } from "@/utils/routerUtil";
import { mapMutations } from "vuex";
import { encrypt } from "@/utils/Objects";
export default {
  name: "Login",
  components: { CommonLayout },
  data() {
    return {
      disabled: false,
      time: 60,
      tabIndex: "1",
      logging: false,
      error: "",
      form: this.$form.createForm(this),
    };
  },
  computed: {
    systemName() {
      return this.$store.state.setting.systemName;
    },
  },
  created() {
    this.$nextTick(() => {
      this.form.setFieldsValue({
        phone: "",
        password: "",
      });
    });
  },
  methods: {
    callback(key) {
      this.tabIndex = key;
    },
    //倒计时
    handelTime() {
      this.disabled = true;
      var timer = setInterval(() => {
        if (this.time == 0) {
          this.time = 60;
          clearInterval(timer);
          this.disabled = false;
        } else {
          this.time = this.time - 1;
        }
      }, 1000);
    },
    //获取验证码
    getMail() {
      const phone = this.form.getFieldValue("phone1");

      getLoginCode({ phone: phone }).then((res) => {
        if (res.retcode == "success") {
          this.handelTime();
          this.$message.success("验证码发送成功");
        }
      });
    },
    ...mapMutations("account", ["setUser", "setPermissions", "setRoles"]),
    onSubmit(e) {
      e.preventDefault();
      this.form.validateFields((err) => {
        if (!err) {
          this.logging = true;
          const phone =
            this.tabIndex == 1
              ? this.form.getFieldValue("phone")
              : this.form.getFieldValue("phone1");
          const password =
            this.tabIndex == 1
              ? this.form.getFieldValue("password")
              : this.form.getFieldValue("password1");
          if (this.tabIndex == 1) {
            getKey(phone).then(({ data }) => {
              let pwd = encrypt(password, data);
              login({ loginType: this.tabIndex, password: pwd, phone: phone })
                .then(this.afterLogin)
                .catch(() => {
                  this.logging = false;
                });
            });
          }
          if (this.tabIndex == 2) {
            login({ loginType: this.tabIndex, code: password, phone: phone })
              .then(this.afterLogin)
              .catch(() => {
                this.logging = false;
              });
          }
        }
      });
    },
    afterLogin(res) {
      console.log("zzzz");
      this.logging = false;
      const loginRes = res;
      if (loginRes.retcode === "success") {
        const { roleId, roleName, token } = loginRes.data;
        this.setUser(loginRes.data);
        this.setPermissions(roleName);
        this.setRoles(JSON.stringify({ roleId, roleName }));
        setAuthorization({
          token,
          expireAt: new Date(loginRes.data.expireAt),
        });
        // 获取路由配置
        // getRoutesConfig().then((result) => {
        //   const routesConfig = result.data.data;
        //   loadRoutes(routesConfig);
        this.$router.push("/home");
        //   this.$message.success(loginRes.retmsg, 3);
        // });
        this.error = "";
      } else {
        this.error = loginRes.retmsg;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.ant-tabs {
  color: #ffff !important;
}
.common-layout {
  .top {
    text-align: center;
    .header {
      height: 44px;
      line-height: 44px;
      margin-bottom: 50px;
      a {
        text-decoration: none;
      }
      .logo {
        height: 44px;
        vertical-align: top;
        margin-right: 16px;
      }
      .title {
        font-size: 33px;
        color: @title-color;
        font-family: "Myriad Pro", "Helvetica Neue", Arial, Helvetica,
          sans-serif;
        font-weight: 600;
        position: relative;
        top: 2px;
        color: #fff;
      }
    }
    .desc {
      font-size: 14px;
      color: @text-color-second;
      margin-top: 12px;
      margin-bottom: 40px;
    }
  }
  .login {
    width: 368px;
    margin: 0 auto;
    @media screen and (max-width: 576px) {
      width: 95%;
    }
    @media screen and (max-width: 320px) {
      .captcha-button {
        font-size: 14px;
      }
    }
    .icon {
      font-size: 24px;
      color: @text-color-second;
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: @primary-color;
      }
    }
  }
}
</style>
